package com.xsy.localgame.puzzle;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xsy.localgame.R;

public class PuzzleActivity extends AppCompatActivity implements View.OnClickListener{

    private int mCurrentLevel = 3;
    private GamePintuLayout gameView;
    private TextView level;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_puzzle);

        ImageView image = findViewById(R.id.image);
        Button btnStart = findViewById(R.id.btn_start);
        ImageView imageDelet = findViewById(R.id.image_delect);
        ImageView imageAdd = findViewById(R.id.image_add);
        gameView = findViewById(R.id.gameView);
        level = findViewById(R.id.level);

        String imageStr = getIntent().getStringExtra("image_path");
        int imageUrl = getIntent().getIntExtra("image_url",0);

        if (!TextUtils.isEmpty(imageStr)) {
            Bitmap bitmap = BitmapFactory.decodeFile(imageStr);
            image.setImageBitmap(bitmap);
            gameView.setBitMap(bitmap);
        } else if (imageUrl != 0) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imageUrl);
            image.setImageBitmap(bitmap);
            gameView.setBitMap(bitmap);
        } else {
            image.setImageResource(R.mipmap.image_default);
        }

        btnStart.setOnClickListener(this);
        imageDelet.setOnClickListener(this);
        imageAdd.setOnClickListener(this);
        gameView.setLevelListener(level1 -> {
            mCurrentLevel = level1;
            level.setText(String.valueOf(mCurrentLevel));
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        if (id == R.id.btn_start) {
            gameView.setLevel(mCurrentLevel);
        } else if (id == R.id.image_add) {
            if (mCurrentLevel >= 12) {
                Toast.makeText(this, "The highest level is 12", Toast.LENGTH_SHORT).show();
                return;
            }
            mCurrentLevel++;
            level.setText(String.valueOf(mCurrentLevel));
        } else if (id == R.id.image_delect) {
            if (mCurrentLevel <= 3) {
                Toast.makeText(this, "The minimum level is 3", Toast.LENGTH_SHORT).show();
                return;
            }
            mCurrentLevel--;
            level.setText(String.valueOf(mCurrentLevel));
        }
    }
}